const app = getApp()

Page({


    data:{
        page:1,
        loadData:false,
        array: [
            {
                id:0,
                name:'全部'
            },
            {
                id: 1,
                name: '铝模优秀班组'
            },
            {
                id: 2,
                name: '市政优秀班组'
            },
            {
                id: 3,
                name: '土建优秀班组'
            },
            {
                id: 4,
                name: '其他'
            }
        ],
        region:[],
        customItem: '全部',
        src:'https://developers.weixin.qq.com/miniprogram/dev/image/cat/0.jpg?t=18082016',
        mode:'aspectFill',
        temp : [],
        end:false,
        type: 0,
        province:'',
        city:'',
        area:''
    },
    onLoad(){
        this.getData(this.data.page)
    },
    onPullDownRefresh (){
        this.setData({
            page:1,
            temp:[],
            empty:true,
            end:false
        })
        this.getData(this.data.page)
        wx.stopPullDownRefresh()
    },
    onReachBottom(){
        if (!this.data.end){
            this.setData({
                page:this.data.page+1
            })
            this.getData(this.data.page)
        }
    },
    getData(page){
        if (!this.data.loadData){
            let {province,city,area,type} = this.data
            this.setData({
                loadData:true
            })
            app.axios('/craftsman','GET',response=>{
                if (this.data.page>1) {
                    if (response.list.length>0) {
                        this.setData({
                            temp: [...this.data.temp, ...response.list],
                            empty:false
                        })
                        if (response.list.length<10){
                            this.setData({
                                end:true
                            })
                        }
                    }else {
                        this.setData({
                            end:true
                        })
                    }
                }else {
                    if (response.list.length>0){
                        this.setData({
                            temp: [...this.data.temp, ...response.list],
                            empty:false
                        })
                        if (response.list.length<10){
                            this.setData({
                                end:true
                            })
                        }
                    }
                }
                this.setData({
                    loadData:false
                })
            },{page,province,city,area,type})
        }

    },
    switchMap(){
        wx.redirectTo({
          url: '/pages/map/index'
        })
    },
    cityCode(obj){
        let codeArray = obj.code
        let valueArray = obj.value
        let data = [];
        for (let i=0;i<3;i++){
            let obj = {}
            obj.code = codeArray[i];
            obj.name = valueArray[i];
            data.push(obj)
        }
        return data;
    },
    bindRegionChange: function (e) {
        let data = this.cityCode(e.detail);
        console.log(data)
        // if ( !this.cityCheck(data) ){
        //     this.showError('请选择正确区域')
        // }else {
            this.setData({
                province: data[0].code?data[0].code:0,
                city: data[1].code?data[1].code:0,
                area: data[2].code?data[2].code:0,
                region:data
            })
            this.setData({
                page:1,
                temp:[],
                empty:true,
                end:false
            })
            this.getData(this.data.page)
        // }
    },
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            type: e.detail.value
        })
        this.setData({
            page:1,
            temp:[],
            empty:true,
            end:false
        })
        this.getData(this.data.page)
    },
    details(e){
        wx.navigateTo({
            url: '/pages/workerDetails/index'+'?id='+e.currentTarget.dataset.id
        })
    },
    cityCheck(region){
        let res = true
        console.log(region)
        region.forEach(item=>{
            if (item.code === undefined ){
                res = false
            }
        });
        return res
    },
    showError(msg){
        wx.showModal({
            title: '错误',
            content: msg,
            showCancel: false
        })
    },
})
